<?php

namespace app\model\order;

use app\model\user\User;
use think\Model;

/**
 * t_order 订单主表
 * @property integer $id 订单id(主键)
 * @property integer $pid 父订单id
 * @property integer $type 订单类型 1:普通订单 2:拼团订单 3:秒杀订单 4:砍价订单
 * @property string $order_no 订单号
 * @property integer $user_id 下单用户
 * @property integer $total_num 订单商品数量
 * @property string $freight_price 邮费
 * @property string $order_price 订单总金额
 * @property integer $pay_way 支付方式 1:微信 2:支付宝 3:余额
 * @property string $pay_price 实际支付金额
 * @property string $vip_discount 会员优惠
 * @property string $coupon_amount 优惠券累计抵扣金额
 * @property integer $pay_status 支付状态 1:待支付 2:已支付 3:已退款 4:部分退款 5:部分支付 6:支付异常
 * @property string $pay_time 支付时间
 * @property string $delivery_time 发货时间
 * @property string $cancel_time 取消时间
 * @property string $received_time 收货时间
 * @property string $close_time 关闭时间
 * @property integer $status 订单状态 1:待确认 2:待支付 3:待发货 4:待收货 5:部分发货 6:交易完成 7:已取消 8:已关闭 9:库存不足
 * @property string $source 订单来源
 * @property integer $refund_status 退款状态 1:未退款 2:审批中 3:已退款
 * @property string $refunded_price 已退款金额
 * @property integer $refunded_num 已退款数量
 * @property string $return_msg 第三方返回信息
 * @property string $third_code 第三方支付订单号
 * @property integer $user_comments 用户是否评价 1:未评价 2:已评价
 * @property string $remark 用户备注
 * @property integer $is_del 后台是否删除 1:正常 2:删除
 * @property integer $user_del 用户是否删除 1:正常 2:删除
 * @property integer $experience 赠送的经验值
 * @property integer $integral 赠送的积分
 * @property string $create_time 创建时间
 * @property string $update_time 更新时间
 */
class Order extends Model
{
    /**
     * The connection name for the model.
     *
     * @var string|null
     */
    protected $connection = 'mysql';

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 't_order';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $pk = 'id';

    //订单类型 1:普通订单 2:拼团订单 3:秒杀订单 4:砍价订单
    const TYPE_NORMAL = 1;
    const TYPE_PINTUAN = 2;
    const TYPE_SECKILL = 3;

    //支付方式 1:微信 2:支付宝 3:余额
    const PAY_WAY_WECHAT_PAY = 1; //微信
    const PAY_WAY_ALI_PAY = 2; //支付宝
    const PAY_WAY_BALANCE = 3; //余额

    //支付状态 1:待支付 2:已支付 3:已退款 4:部分退款 5:部分支付 6:支付异常
    const PAY_STATUS_WAIT = 1;  //待支付
    const PAY_STATUS_PAID = 2;  //已支付
    const PAY_STATUS_REFUND = 3;    //已退款
    const PAY_STATUS_SPLIT_REFUND = 4;  //部分退款
    const PAY_STATUS_SPLIT_PAID = 5;    //部分支付
    const PAY_STATUS_SPLIT_PAY_ERR = 6; //支付异常

    //订单状态 1:待确认 2:待支付 3:待发货 4:待收货 5:部分发货 6:交易完成 7:已取消 8:已关闭 9:库存不足
//    const STATUS_WAIT = 1;  //待确认
    const STATUS_WAIT_PAID = 2;     //待支付
    const STATUS_WAIT_DELIVERY = 3;     //待发货
    const STATUS_WAIT_TAKE_DELIVERY = 4;    //待收货 完整发货
    const STATUS_SPLIT_DELIVERY = 5;    //部分发货
    const STATUS_TRANSACTION_COMPLETION = 6;    //交易完成
    const STATUS_CANCEL = 7;    //已取消  手动取消
    const STATUS_CLOSE = 8; //已关闭  超时关闭
//    const STATUS_STOCK_OUT = 9; //库存不足

    //退款状态 1:未退款 2:审批中 3:已退款
    const REFUND_STATUS_NORMAL = 1;  //未退款
    const REFUND_STATUS_REVIEW_ING = 2;     //审批中
    const REFUND_STATUS_REFUNDED = 3;     //已退款

    const STATUS_TEXT_MAP = [
//        self::STATUS_WAIT => "待确认",
        self::STATUS_WAIT_PAID => "待支付",
        self::STATUS_WAIT_DELIVERY => "待发货",
        self::STATUS_WAIT_TAKE_DELIVERY => "待收货",
        self::STATUS_SPLIT_DELIVERY => "部分发货",
        self::STATUS_TRANSACTION_COMPLETION => "交易完成",
        self::STATUS_CANCEL => "已取消",
        self::STATUS_CLOSE => "已关闭",
//        self::STATUS_STOCK_OUT => "库存不足",
    ];

    const PAY_STATUS_TEXT_MAP = [
        self::PAY_STATUS_WAIT => "待支付",
        self::PAY_STATUS_PAID => "已支付",
        self::PAY_STATUS_REFUND => "已退款",
        self::PAY_STATUS_SPLIT_REFUND => "部分退款",
        self::PAY_STATUS_SPLIT_PAID => "部分支付",
        self::PAY_STATUS_SPLIT_PAY_ERR => "支付异常",
    ];
    const PAY_WAY_TEXT_MAP = [
        self::PAY_WAY_WECHAT_PAY => "微信",
        self::PAY_WAY_ALI_PAY => "支付宝",
        self::PAY_WAY_BALANCE => "余额",
    ];
    const TYPE_TEXT_MAP = [
        self::TYPE_NORMAL => "普通订单",
        self::TYPE_PINTUAN => "拼团订单",
        self::TYPE_SECKILL => "秒杀订单",
//        self::TYPE_KA => "砍价订单",
    ];

    const REFUND_STATUS_TEXT_MAP = [
        self::REFUND_STATUS_NORMAL => "未退款",
        self::REFUND_STATUS_REVIEW_ING => "退款中",
        self::REFUND_STATUS_REFUNDED => "已退款",
    ];

    protected $append = [
        'status_text',
        'pay_way_text',
        'type_text',
        'pay_status_text',
        'refund_status_text',
    ];

    public function user()
    {
        return $this->belongsTo(User::class, "user_id", "id");
    }

    public function detail()
    {
        return $this->hasMany(OrderDetail::class, "order_id", "id");
    }

    public function address()
    {
        return $this->hasOne(OrderAddress::class, "order_id", "id");
    }


    public function getStatusTextAttr($value, $data)
    {
        return isset($data['status']) ? self::STATUS_TEXT_MAP[$data['status']] ?? "" : "";
    }

    public function getPayWayTextAttr($value, $data)
    {
        return isset($data['pay_way']) ? self::PAY_WAY_TEXT_MAP[$data['pay_way']] ?? "" : "";
    }

    public function getTypeTextAttr($value, $data)
    {
        return isset($data['type']) ? self::TYPE_TEXT_MAP[$data['type']] ?? "" : "";
    }

    public function getPayStatusTextAttr($value, $data)
    {
        return isset($data['pay_status']) ? self::PAY_STATUS_TEXT_MAP[$data['pay_status']] ?? "" : "";
    }

    public function getRefundStatusTextAttr($value, $data)
    {
        return isset($data['refund_status']) ? self::REFUND_STATUS_TEXT_MAP[$data['refund_status']] ?? "" : "";
    }

}
